#pragma once
#include "interfaces/GuiPlugin.h"
#include <QToolBar>
#include <QFileDialog>
#include "StarlabMainWindow.h"

#undef DEB
#ifdef DEBUG_THIS_PLUGIN
    #define DEB 
#else
    #define DEB if(false)
#endif

class gui_filemenu : public GuiPlugin{
    Q_OBJECT
    Q_INTERFACES(GuiPlugin)
    
private:
    StarlabMainWindow* mainWindow;
    
private:
    void notImplementedException(){
        throw StarlabException("not implemented");
    }
    
private slots:
    /// Smart open (can open either project or files)
    void open();
    /// Smart save (can decide to save either project or files)
    void save();
    void save_project(){ qWarning("not implemented"); }
    void save_project_as(){ qWarning("not implemented"); }
    void save_selection(){ qWarning("not implemented"); }
    void save_selection_as(){ qWarning("not implemented"); }
    void reload_project(){ qWarning("not implemented"); }
    void reload_selection();
    
public:    
    void load(StarlabMainWindow* window){
        this->mainWindow = window;
        // bool docNotEmpty = !window->document()->isEmpty();

        /// Opens a model/project
        { 
            QAction* action = new QAction(QIcon(":/images/import_mesh.png"),tr("Open (Project or Model)"), this);
            action->setShortcutContext(Qt::ApplicationShortcut);
            action->setShortcut(Qt::CTRL+Qt::Key_O); ///< @todo change to cross platform open
            connect(action, SIGNAL(triggered()), this, SLOT(open()));            
            window->fileMenu->addAction(action);
            window->mainToolBar->addAction(action);
        }     

        /// Smart save
        { 
            QAction* action = new QAction(QIcon(":/images/save.png"),tr("Save (Project or Model)"), this);
            action->setShortcutContext(Qt::ApplicationShortcut);
            action->setShortcut(Qt::CTRL+Qt::Key_S); ///< @todo change to cross platform save
            connect(action, SIGNAL(triggered()), this, SLOT(save()));
            window->fileMenu->addAction(action);
            window->mainToolBar->addAction(action);
        }
        
        /// @todo Saves a project (and recursively save all models under)
        if(false){ 
            QAction* action = new QAction(QIcon(":/images/save.png"),tr("Save Project"), this);
            connect(action, SIGNAL(triggered()), this, SLOT(save_project()));
            window->fileMenu->addAction(action);
        }
        
        /// @todo Saves a project as...
        if(false){ 
            QAction* action = new QAction(QIcon(":/images/save.png"),tr("Save Project As.."), this);
            connect(action, SIGNAL(triggered()), this, SLOT(save_project_as()));
            window->fileMenu->addAction(action);
        }
        
        /// @todo Save selection
        if(false){ 
            QAction* action = new QAction(QIcon(":/images/save.png"),tr("Save Selection"), this);
            connect(action, SIGNAL(triggered()), this, SLOT(save_selection()));
            window->fileMenu->addAction(action);
        }
        /// @todo Save selection as...
        if(false){ 
            QAction* action = new QAction(QIcon(":/images/save.png"),tr("Save Selection as..."), this);
            connect(action, SIGNAL(triggered()), this, SLOT(save_selection_as()));
            window->fileMenu->addAction(action);
        }
        
        window->fileMenu->addSeparator();
        
        /// @todo Revert project (and models recursively)
        if(false){
            QAction* action = new QAction(QIcon(":/images/reload.png"),tr("Revert Project"), this);
            connect(action, SIGNAL(triggered()), this, SLOT(reload_project()));
            window->fileMenu->addAction(action);
        }
        
        /// Revert (selection)
        if(true){
            QAction* action = new QAction(QIcon(":/images/reload.png"),tr("Revert Selection"), this);
            action->setShortcutContext(Qt::ApplicationShortcut);
            action->setShortcut(Qt::CTRL+Qt::Key_R);
            connect(action, SIGNAL(triggered()), this, SLOT(reload_selection()));
            window->fileMenu->addAction(action);
            window->mainToolBar->addAction(action);
        }
        
        /// Quit
        {
            QAction* action = new QAction(tr("E&xit"), this);
            action->setShortcut(Qt::CTRL+Qt::Key_Q);
            connect(action, SIGNAL(triggered()), window, SLOT(close()));
            window->fileMenu->addAction(action);
        }
    }
};


// importMeshAct->setEnabled(activeDoc);
// exportMeshAct->setEnabled(activeDocNotEmpty);
// exportMeshAsAct->setEnabled(activeDocNotEmpty);
// reloadMeshAct->setEnabled(activeDocNotEmpty);
// saveProjectAsAct->setEnabled(activeDoc);
// saveProjectAct->setEnabled(activeDoc);
// closeProjectAct->setEnabled(activeDoc);

// mainToolBar->addAction(saveSnapshotAct);

//saveProjectAsAct = new QAction(QIcon(":/images/save.png"),tr("Save Project As..."), this);
//connect(saveProjectAsAct, SIGNAL(triggered()), this, SLOT(saveProject()));

//exportMeshAct = new QAction(QIcon(":/images/save.png"),tr("&Export Mesh..."), this);
//exportMeshAct->setShortcutContext(Qt::ApplicationShortcut);
//exportMeshAct->setShortcut(Qt::CTRL+Qt::Key_E);
//connect(exportMeshAct, SIGNAL(triggered()), this, SLOT(save()));

//exportMeshAsAct = new QAction(QIcon(":/images/save.png"),tr("&Export Mesh As..."), this);
//connect(exportMeshAsAct, SIGNAL(triggered()), this, SLOT(saveAs()));

